//
//  ProductViewController.swift
//  Skeleton
//
//  Created by bin hu on 2025/7/4.
//

import UIKit

class ProductViewController: UIViewController {
    
    var index:Int = 1
    
    private lazy var tableView:UITableView = {
        let tableV = UITableView(frame: view.bounds, style: .grouped)
        tableV.backgroundColor = .clear
        tableV.delegate = self
        tableV.dataSource = self
        tableV.showsVerticalScrollIndicator = false
        if #available(iOS 15.0, *) {
            tableV.sectionHeaderTopPadding = 0
        } else {}
        tableV.contentInsetAdjustmentBehavior = .never
        view.addSubview(tableV)
        return tableV
    }()
    
    private lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (view.bounds.width - 48) / 2, height: 200)
        layout.minimumInteritemSpacing = 16
        layout.minimumLineSpacing = 16
        layout.sectionInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
        layout.scrollDirection = .vertical
        let collectionV = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        collectionV.backgroundColor = .clear
        collectionV.delegate = self
        collectionV.dataSource = self
        collectionV.scrollsToTop = true
        collectionV.contentInset = UIEdgeInsets(top: 30.0, left: 0, bottom: 0, right: 0)
        view.addSubview(collectionV)
        return collectionV
    }()
    
    private lazy var contentView:UIView = {
        let v = UIView()
        view.addSubview(v)
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        switch index {
            case 1:
                setupTableView()
            case 2:
                setupCollectionView()
            default:
                setupTableView()
        }
    }
    
    private func setupTableView() {
        tableView.register(ProductTableViewCell.self, forCellReuseIdentifier: "ProductCell")
        tableView.rowHeight = 80
        tableView.separatorStyle = .none
        
//        tableView.showSkeleton(exclude: [IndexPath(row: 0, section: 0)], dataSource: self)
        tableView.showSkeletonWithDataSource()
        // 3秒后自动隐藏
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.tableView.hideTabSkeleton()
        }
    }
    
    private func setupCollectionView() {
        
        collectionView.register(ProductCollectionViewCell.self, forCellWithReuseIdentifier: "ProductCell")
        
//        collectionView.showSkeleton(exclude: [IndexPath(item: 1, section: 0)], dataSource: self)
        
        collectionView.showSkeletonWithDataSource()
        // 3秒后自动隐藏
        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.collectionView.hideTabSkeleton()
        }
    }

}

extension ProductViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell", for: indexPath) as! ProductTableViewCell
        return cell
    }
}

extension ProductViewController:UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath) as! ProductCollectionViewCell
        return cell
    }
}
